<template>
  <el-form class="tl-form" ref="form" label-width="150px">
    <!-- <div class="statistic-title">
      <span>累计拜访人次：{{ visitcount }}&nbsp;&nbsp;&nbsp;&nbsp;</span>
      <span>要求&nbsp;<el-input size="mini" class="numrule" style="width: 50px" type="number" v-model.number="model.visitnum" :min="0" oninput="this.value=this.value.match(/\d{0,}/)" @blur="saveVisitNum" :disabled="!isPM"></el-input>&nbsp;次&nbsp;&nbsp;&nbsp;&nbsp;</span>
      <span>有效专家意见数量：{{ visitcountenable }}&nbsp;&nbsp;&nbsp;&nbsp;</span>
      <span>要求&nbsp;<el-input size="mini" class="numrule" style="width: 50px" type="number" v-model.number="model.enablevisitnum" :min="0" oninput="this.value=this.value.match(/\d{0,}/)" @blur="saveVisitNum" :disabled="!isPM"></el-input>&nbsp;条</span>
    </div> -->
    <div class="dialog-footer" style="float: right;margin-right: 10px">
      <el-button type="primary" @click="doSave" :loading="saveLoading || submitLoading"> <svg-icon icon-class="save" />保存 </el-button>
      <tl-attachment sourceType="MpmProjectVisitExpert" :sourceId="projectinfo.id" :editable="canEditAttachment"></tl-attachment>
      <el-button type="primary" @click="doSubmitApproval" :loading="saveLoading || submitLoading"> <svg-icon icon-class="send" />提交审批 </el-button>
      <el-button type="primary" @click="$refs.history.visible = true"> <svg-icon icon-class="list" />审批历史 </el-button>
    </div>
    <el-button type="text" icon="el-icon-circle-plus" size="medium" @click="addVisitExpert" :disabled="!canSeenAdd">新增评标专家拜访反馈</el-button>
    <el-timeline>
      <el-timeline-item v-for="(item, index) in visitExpertList" :key="index" color="#0A6ED2" style="padding-bottom: 25px">
        <tl-edit-one :ref="'visitExpert' + index" :newValue="item" :index="index" @refresh="queryVisitExpertList()" @deleteList="deleteList(index)"></tl-edit-one>
      </el-timeline-item>
    </el-timeline>
    <tl-approval-history ref="history" sourceType="MpmProjectVisitExpert" :sourceId="approvalId" />
  </el-form>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
import { validateNull } from '@tapui/utils/validate'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
import tlEditOne from './editOne'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlEditOne },
  data() {
    return {
      moduleTitle: '评分细则专家拜访',
      urlPrefix: '/mpm/projectVisitExpert/',
      url: {
        // 额外的url
        queryVisitExpertList: '/mpm/projectVisitExpert/queryVisitExpertList',
        saveVisitExpert: '/mpm/projectVisitExpert/saveVisitExpert',
        delete: '/mpm/projectVisitExpert/delete',
        queryVisitNum: '/mpm/projectExpertFull/list',
        changeExpertNum: '/mpm/projectExpertFull/changeExpertNum'
      },
      visitExpertList: [],
      // 审批历史id
      approvalId: 0,
      visitnum: 3,
      enablevisitnum: 1
    }
  },
  props: {
    render: {
      default: 'div',
      type: String
    },
    projectinfo: {
      type: Object,
      default: () => {}
    },
    bidscoretimes: {
      default: '1',
      type: String
    }
  },
  computed: {
    visitcount() {
      return this.visitExpertList.length
    },
    visitcountenable() {
      return this.visitExpertList.filter((item) => item.canreal === '1').length
    },
    canSeenAdd() {
     return this.visitExpertList.filter((item) => item.state === 1).length === 0
    },
    canEditAttachment() {
      return this.visitExpertList.filter((item) => item.state === 0 || item.state === 2 || validateNull(item.state)).length > 0
    },
    isPM() {
      if (this.$store.getters.projectroles) {
        return this.$store.getters.projectroles.indexOf('pm') > -1
      } else {
        return false
      }
    }
  },
  methods: {
    assignDefaultValue() {
      let value = {}
      if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
        this.$set(value, 'flowid', parseInt(this.codeModel.flowidList[0].id))
      }
      this.$set(value, 'bidscoretimes', this.bidscoretimes)
      this.$set(value, 'orgcode', this.org.code)
      this.$set(value, 'orgname', this.org.name)
      this.$set(value, 'deptcode', this.dept.code)
      this.$set(value, 'deptname', this.dept.fullname)
      this.$set(value, 'usercode', this.user.code)
      this.$set(value, 'username', this.user.name)
      this.$set(value, 'projectcode', this.projectinfo.projectcode)
      return value
    },
    // 获取评标专家拜访反馈信息
    async queryVisitExpertList() {
      return await getAction(this.url.queryVisitExpertList, { projectcode: this.projectinfo.projectcode, bidscoretimes: this.bidscoretimes }).then((res) => {
        if (res.success) {
          this.visitExpertList = res.result.visitexpert
          this.approvalId = res.result.approvalid ? res.result.approvalid : 0
        } else {
          this.$tap.alert('获取评标专家拜访反馈信息失败，请联系管理员！', 'warning')
        }
      })
    },
    // 删除评标专家拜访反馈信息
    deleteList(index) {
      if (this.visitExpertList[index].state === 0 || this.visitExpertList[index].state === 2) {
          deleteAction(this.url.delete, { ids: this.visitExpertList[index].id }).then((res) => {
            if (res.success) {
              this.$tap.message(res.message, 'success')
              this.visitExpertList.splice(index, 1)
            } else {
              this.$tap.alert(res.message, 'warning')
            }
          })
      } else {
         this.visitExpertList.splice(index, 1)
      }
    },
    // 添加评标专家拜访反馈
    addVisitExpert() {
      let value = this.assignDefaultValue()
      this.visitExpertList.unshift(value)
    },
    // 提交审批，将评标专家中开立、退回的单据存储并提交审批
    async doSubmitApproval() {
      // 判断是否可以提交审批：(1、有新增反馈2、必填字段全部填写)
      let visitexpert = this.visitExpertList.filter(item => validateNull(item.state) || item.state === 0 || item.state === 2)
      if (visitexpert.length === 0) {
        this.$tap.alert('当前无可提交审批内容，请确认！', 'warning')
        return false
      }
      for (let visit of visitexpert) {
        if (validateNull(visit.visitdate) || validateNull(visit.expertpost) || validateNull(visit.visitname) || validateNull(visit.expertadvise)) {
          this.$tap.alert('评标专家拜访反馈内容需填写完整！', 'warning')
          return false
        }
      }
      let flag = await this.doSave()
      if (flag) {
        let submitList = this.visitExpertList.filter(item => validateNull(item.state) || item.state === 0 || item.state === 2)
        let submitModel = submitList[0]
        this.submitLoading = true
        putAction(this.url.submit, submitModel).then((res) => {
            if (res.success) {
              this.queryVisitExpertList()
              this.$tap.message('提交审批成功！', 'success')
              this.submitLoading = false
            } else {
              this.$tap.alert(res.message, 'warning')
              this.submitLoading = false
            }
        })
      }
    },
    async doSave() {
      this.saveLoading = true
      let visitexpert = this.visitExpertList.filter(item => validateNull(item.state) || item.state === 0 || item.state === 2)
      if (visitexpert.length === 0) {
        this.$tap.alert('当前无可保存内容，请确认！', 'warning')
        this.saveLoading = false
        return false
      }
      return await postAction(this.url.saveVisitExpert, { visitexpert: visitexpert }).then(async res => {
          if (res.success) {
            return await this.queryVisitExpertList().then(async res => {
              this.$tap.message('保存成功！', 'success')
              this.saveLoading = false
              return true
            })
          } else {
            this.$tap.alert('保存失败，请联系管理员！', 'warning')
            this.saveLoading = false
            return false
          }
      })
    },
    queryVisitNum() {
      getAction(this.url.queryVisitNum, { qry_projectcode: this.projectinfo.projectcode }).then((res) => {
        if (res.success) {
          if (res.result.dataList.length > 0) {
            this.visitnum = res.result.dataList[0].visitnum
            this.enablevisitnum = res.result.dataList[0].enablevisitnum
          }
          this.$set(this.model, 'visitnum', this.visitnum)
          this.$set(this.model, 'enablevisitnum', this.enablevisitnum)
        } else {
          this.$tap.alert('获取拜访要求数据失败，请联系管理员！', 'warning')
        }
      })
    },
    saveVisitNum() {
      let value1 = this.model.visitnum
      let value2 = this.model.enablevisitnum
      if (parseInt(value1) < parseInt(this.visitnum) || parseInt(value2) < parseInt(this.enablevisitnum)) {
          this.$tap.alert('输入值不得小于原始值！', 'warning')
          this.$set(this.model, 'visitnum', this.visitnum)
          this.$set(this.model, 'enablevisitnum', this.enablevisitnum)
      }
      if (parseInt(value1) > parseInt(this.visitnum) || parseInt(value2) > parseInt(this.enablevisitnum)) {
        const param = {
          projectcode: this.projectinfo.projectcode,
          visitnum: value1,
          enablevisitnum: value2
        }
        postAction(this.url.changeExpertNum, param).then(res => {
          if (res.success) {
            this.$tap.message('保存成功!', 'success')
            this.visitnum = value1
            this.enablevisitnum = value2
          } else {
            this.$tap.alert('保存失败，请联系管理员!', 'warning')
          }
        })
      }
    }
  },
  created() {
    this.queryVisitExpertList()
    // this.queryVisitNum()
  }
}
</script>
<style lang="scss" scoped>
.tl-form /deep/ .statistic-title span {
  font-size: 16px;
  font-weight: bold;
}
/deep/.numrule input::-webkit-outer-spin-button,
/deep/.numrule input::-webkit-inner-spin-button {
  -webkit-appearance: none!important;
}
/deep/.numrule input[type="number"]{
  -moz-appearance: textfield;
}
</style>
